* Reset settings and initialize log file
launch, path("share/multiple_kids")

*-------------------------------------------------------------------------------
* Price and Wasserman (2024), "The Summer Drop in Female Employment"
*
* Description: Show heterogeneity by number of children of different ages.
*-------------------------------------------------------------------------------


* Prepare data and run models
*-------------------------------------------------------------------------------

if "$estimate" != "0" {
	* Load a sample of parents
	gzuse "$basepath/data/derived/cps_bms_sample.dta.gz", clear

	* Tally up children by ages
	gen kids00to05 = 0
	gen kids06to12 = 0
	gen kids13to17 = 0

	forvalues k = 0/5 {
		replace kids00to05 = kids00to05 + ownkids_age`k'
	}

	forvalues k = 6/12 {
		replace kids06to12 = kids06to12 + ownkids_age`k'
	}

	forvalues k = 13/17 {
		replace kids13to17 = kids13to17 + ownkids_age`k'
	}

	gen totkids = kids00to05 + kids06to12 + kids13to17

	* Classify (some) parents by number and ages of their children
	gen byte gp = .
	replace gp = 1 if missing(gp) & totkids == 1 & kids00to05 == 1
	replace gp = 2 if missing(gp) & totkids == 1 & kids06to12 == 1
	replace gp = 3 if missing(gp) & totkids == 1 & kids13to17 == 1
	replace gp = 4 if missing(gp) & totkids == 2 & kids00to05 == 2
	replace gp = 5 if missing(gp) & totkids == 2 & kids06to12 == 2
	replace gp = 6 if missing(gp) & totkids == 2 & kids13to17 == 2

	* Report the share of mothers affiliated with education
	tab school if female == 1 & gp == 1 & !inrange(month, 6, 8) [aw = wtfinl]
	tab school if female == 1 & gp == 2 & !inrange(month, 6, 8) [aw = wtfinl]
	tab school if female == 1 & gp == 3 & !inrange(month, 6, 8) [aw = wtfinl]
	tab school if female == 1 & gp == 4 & !inrange(month, 6, 8) [aw = wtfinl]
	tab school if female == 1 & gp == 5 & !inrange(month, 6, 8) [aw = wtfinl]
	tab school if female == 1 & gp == 6 & !inrange(month, 6, 8) [aw = wtfinl]

	* Retain women assigned to specific groups
	keep if !missing(gp)

	* Aggregate
	gcollapse (mean) emp (rawsum) wtfinl (first) month tmspline* weeks [pw = wtfinl], by(female gp tm)
	tempfile core
	save `core'

	* Estimate specifications
	foreach f of numlist 0 1 {
		forvalues g = 1/6 {
			* Reload sample
			use if female == `f' & gp == `g' using `core', clear
			quietly tsset tm

			* Run specification
			quietly ivreg2 emp ib5.month tmspline* weeks [aw = wtfinl], bw($bandwidth) robust small
			process_estimates, path("multiple_kids") model("f`f'_gp`g'")
		}
	}
}


* Prepare estimates
*-------------------------------------------------------------------------------

* Load estimates into memory
load_estimates, path("multiple_kids")

* Prepare coefficient labels for each month
make_coeflabels


* Plot seasonality in employment
*-------------------------------------------------------------------------------

* Prepare background shading
clear
set obs 14
gen rlow = -4.1
gen rupp = +2.1
gen rval = _n - 0.5

* Loop over sex
foreach f of numlist 0 1 {
	if `f' == 0 local flbl men
	if `f' == 1 local flbl women

	* Loop over child ages
	forvalues k = 1/3 {
		local m1 = `k'
		local m2 = `k' + 3

		if `k' == 1 local ktitle "Children under 6 years old"
		if `k' == 2 local ktitle "Children 6 to 12 years old"
		if `k' == 3 local ktitle "Children 13 to 17 years old"

		#delimit ;
		coefplot
			(f`f'_gp`m1', offset(-.05) recast(connected))
			(f`f'_gp`m2', offset(+.05) recast(connected)),
			coeflabels(`coeflabels')
			title("{bf:`ktitle'}")
			xtitle("")
			xlabel(, labsize(*0.925) alternate)
			ytitle("")
			yscale(range(-4.1 2.1))
			ylabel(-4(1)2)
			yline(0)
			addplot(
				rarea rupp rlow rval if inrange(rval, 0.5, 5.5), color($ltgs) plotregion(margin(t=0 b=0)) below ||
				scatteri 0 0.5 0 13.5, recast(line) color(black) below)
			vertical
			legend(rows(1) size(*0.8) label(4 "Exactly one child") label(6 "Exactly two children"))
			rescale(100);
		#delimit cr

		tempfile g`k'
		graph save "`g`k''", replace
	}

	* Create three-panel figure
	grc1leg "`g1'" "`g2'" "`g3'", rows(1) l1title("      Difference relative to May (p.p.)", size(*.8)) imargin(vsmall)
	nicepdf "$basepath/output/multiple_kids_`flbl'.pdf", panels(3) indirect replace
}

* Close the log file
unlaunch
